package com.dong.common.util;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.dong.common.constant.PropertiesConst;
import com.dong.module.user.dao.UserMapper;
import com.dong.module.user.pojo.User;
import com.dong.module.user.service.UserService;
import com.dong.module.user.service.impl.UserServiceImpl;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @description: 用户工具类
 * @author: 陈瑞东
 * @create: 2021-08-25 10:28
 */
public class UserUtils {
    /**
     * 获取是否自动登录标记
     * @return
     */
    public static boolean isAutoLogin() {
        return Boolean.parseBoolean(PropertiesUtils.getValue(PropertiesConst.USER_CONFIG_NAME, PropertiesConst.USER_KEY_AUTOLOGIN));
    }

    /**
     * 获取是否记住我标记
     * @return
     * @throws IOException
     */
    public static boolean isRemember() {
        return Boolean.parseBoolean(PropertiesUtils.getValue(PropertiesConst.USER_CONFIG_NAME, PropertiesConst.USER_KEY_REMEMBERME));
    }

    /**
     * 获取当前用户的用户名和昵称
     * @return
     */
    public static User getUser() {
        UserMapper userMapper = Global.context.getBean(UserMapper.class);
        String id = PropertiesUtils.getValue(PropertiesConst.USER_CONFIG_NAME, PropertiesConst.USER_KEY_ID);
        if (StringUtils.isBlank(id)) {
            return new User();
        }
        User user = userMapper.selectById(id);
        if (user == null) {
            return new User();
        }
        return user;
    }
}
